﻿using System;
using System.Collections.Generic;
using System.Linq;
using RedBranch.Hammock;

namespace CouchAdmin
{
    // this is a couchdb repository using the Relax-net API from RedBranch found at http://code.google.com/p/relax-net/
    // you will need to install the couchbase single server installer located here http://www.couchbase.com/download

    public class CouchRepository
    {
        public Session Session { get; set; }

        private static CouchRepository _instance;
        public static CouchRepository Instance
        {
            get
            {
                if (_instance == null)
                {
                   
                    _instance = new CouchRepository();

                }
                return _instance;
            }
        }

        public void CreateSession(string dbName)
        {
            var c = new Connection(new Uri("http://localhost:5984"));
            Session = c.CreateSession(dbName);
        }

        public void CreateDatabase(string dbName)
        {
            var c = new Connection(new Uri("http://localhost:5984"));
            if (!c.ListDatabases().Contains(dbName))
            {
                c.CreateDatabase(dbName);
            }
        }

        public List<string> GetDatabases()
        {
            List<string> list = new List<string>();
            var c = new Connection(new Uri("http://localhost:5984"));
            c.ListDatabases().ToList().ForEach(delegate(string s)
            {
                if (s != "_replicator" && s != "_users")
                    list.Add(s);
                    
            });
            return list;
        }
    }
}
